/**
 * Copyright(c) 2025 hbswxx.com, All Rights Reserved. Author: kouqiang Create date: 2025/6/5
 */
package cn.sw.syncdata.job.heart;

import cn.sw.syncdata.entity.HardDiskInfo;
import cn.sw.syncdata.util.Util;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Random;

/**
 * description:
 *
 * @author kouqiang
 * @date 2025/6/5 08:35
 */
@Slf4j
@RequiredArgsConstructor
public class ThreadCreateHardDiskInfo implements Runnable {
    private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    private final int total;
    private final String sdate;
    private final String edate;
    private final String fileName;
    private final String[] indexList = {"sw_112_1",
            "sw_123_1",
            "sw_125_1",
            "sw_120_1",
            "sw_126_1",
            "sw_124_1",
            "sw_121_1",
            "sw_127_1",
            "sw_128_1",
            "sw_129_1",
            "sw_130_1",
            "sw_131_1",
            "sw_132_1",
            "sw_133_1",
            "sw_134_1",
            "sw_135_1",
            "sw_136_1"};

    @Override
    public void run() {
        Random random = new Random();
        LocalDateTime sDateTime = LocalDateTime.parse(sdate + "T00:00:00", formatter);
        LocalDateTime eDateTime = LocalDateTime.parse(edate + "T23:59:59", formatter);
        StringBuffer sb = new StringBuffer();
        int indexCount = indexList.length;
        HashSet<String> hashSet = new HashSet<>();
        sb.append("logLevel, logIndexCode, hardDiskName, diskRInfo, diskWInfo, logTime");
        sb.append("\n");
       int alltotal=this.total/2;
        for (int i = 0; i < alltotal; i++) {
            LocalDateTime newDateTime = Util.randomBetween(sDateTime, eDateTime);
            HardDiskInfo newInfo = new HardDiskInfo();
            String logLevel = "INFO";
            double diskRInfo = random.nextDouble() * random.nextInt(3000);
            double diskWInfo = random.nextDouble() * random.nextInt(3000);
            double total = diskWInfo + diskRInfo;
            if (total > 220)
                logLevel = "FATAL";
            else if (total > 180)
                logLevel = "ERROR";
            else if (total > 100)
                logLevel = "WARN";
            newInfo.setLogLevel(logLevel);
            newInfo.setDiskWInfo(String.format("%.2fMB/秒", diskWInfo));
            newInfo.setDiskRInfo(String.format("%.2fMB/秒", diskRInfo));
            newInfo.setHardDiskName("C:");
            newInfo.setLogIndexCode(indexList[random.nextInt(indexCount)]);
            newInfo.setLogTime(newDateTime);
            String line = newInfo.toString();
            if (!hashSet.contains(line)) {
                hashSet.add(line);
                sb.append(line);
                sb.append("\n");
            }
            logLevel = "INFO";
            diskRInfo = random.nextDouble() * random.nextInt(3000);
            diskWInfo = random.nextDouble() * random.nextInt(3000);
            total = diskWInfo + diskRInfo;
            if (total > 220)
                logLevel = "FATAL";
            else if (total > 180)
                logLevel = "ERROR";
            else if (total > 100)
                logLevel = "WARN";
            newInfo.setLogLevel(logLevel);
            newInfo.setDiskWInfo(String.format("%.2fMB/秒", diskWInfo));
            newInfo.setDiskRInfo(String.format("%.2fMB/秒", diskRInfo));
            newInfo.setHardDiskName("D:");
            if (!hashSet.contains(line)) {
                hashSet.add(line);
                sb.append(line);
                sb.append("\n");
            }
            if (i % 2000 == 0) {
                wrteList(sb.toString());
                sb.setLength(0);
            }

        }
        if (sb.length() > 0) {
            wrteList(sb.toString());
        }
    }

    private void wrteList(String lines) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
            writer.write(lines);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
